package com.tfswue.qmp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class RedisConfig {

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        //添加默认配置（30分钟过期）
     RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
             .entryTtl(Duration.ofMinutes(30));

        Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
        //为特定缓存设置独立过期时间
        configMap.put("dataCache",
                RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(1))); //1分钟


//        configMap.put("productCache",
//                RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))); //10分钟

        return RedisCacheManager.builder(factory)
                 .cacheDefaults(defaultConfig)
                 .withInitialCacheConfigurations(configMap)
                 .build();
    }

}
